Use case: OPAMP in follower mode

The use case sets up the OPAMP as a voltage follower to buffer an analog input, ensuring accurate and stable signal transfer, and includes a self-calibration step for optimal operation.

Description of usage

Description

HAL1

HAL2

System Initialization

Initialize HAL library and system peripherals

  • HAL_Init()

  • Configure system clock via SystemClock_Config()

Initialize HAL library and system peripherals

  • HAL_Init()

  • Configure system clock via SystemClock_Config()

GPIO Configuration for OPAMP Input

Enable GPIOx clock

  • __HAL_RCC_GPIOx_CLK_ENABLE()

  • Configure GPIOx pin as Analog mode, No Pull

Enable GPIOx clock

  • HAL_RCC_GPIOx_CLK_ENABLE()

  • Configure GPIOx pin as Analog mode, No Pull

OPAMP Initialization

Set OPAMP parameters individually then:

  • Call HAL_OPAMP_Init(&hopamp1)

Set OPAMP parameters via config struct then:

  • Call HAL_OPAMP_Init(&hopamp1)

  • Call HAL_OPAMP_SetConfig(&hopamp1, &hconfig)

OPAMP Self-Calibration

Call HAL_OPAMP_SelfCalibrate(&hopamp1)

Call HAL_OPAMP_Calibrate(&hopamp1,HAL_OPAMP_POWER_MODE_NORMAL)

Start OPAMP

Call HAL_OPAMP_Start(&hopamp1)

Call HAL_OPAMP_Start(&hopamp1)

Sequence diagram

OPAMP in follower mode

  @startuml
  participant "Main" as Main
  participant "HAL Library" as HAL
  participant "RCC Peripheral" as RCC
  participant "GPIO Peripheral" as GPIO
  participant "OPAMP Peripheral" as OPAMP

  == System Initialization ==
  Main -> HAL: HAL_Init()
  note right: Initialize HAL library\nand system peripherals

  Main -> RCC: SystemClock_Config()
  note right: Configure system clock\nfor proper timing

  == GPIO Configuration for OPAMP Input ==
  Main -> GPIO: Enable GPIOx clock
  note right: __HAL_RCC_GPIOx_CLK_ENABLE()

  Main -> GPIO: Configure GPIOx pin\nMode = Analog, Pull = No Pull
  GPIO --> Main: Pins configured
  note right: Set GPIO pins to analog mode\nDisable digital input/output

  == OPAMP Initialization ==
  Main -> OPAMP: Set Instance = OPAMP1
  Main -> OPAMP: Set Mode = Follower
  Main -> OPAMP: Set NonInvertingInput = IO0
  Main -> OPAMP: Set PowerMode = Normal
  Main -> OPAMP: Set UserTrimming = User
  Main -> HAL: HAL_OPAMP_Init(&hopamp1)
  HAL --> Main: HAL_OK / HAL_ERROR

  note right: Initialize OPAMP with specified parameters

  == OPAMP Self-Calibration ==
  Main -> HAL: HAL_OPAMP_SelfCalibrate(&hopamp1)
  HAL --> Main: HAL_OK / HAL_ERROR

  note right: Perform self-calibration to get trimming values

  == Start OPAMP ==
  Main -> HAL: HAL_OPAMP_Start(&hopamp1)
  HAL --> Main: HAL_OK / HAL_ERROR

  note right: Enable OPAMP to start amplification

  @enduml

Code snippet

HAL1

HAL2

OPAMP_HandleTypeDef hopamp1;

void SystemClock_Config(void);
void Error_Handler(void);

int main(void)
{
    HAL_Init();
    SystemClock_Config();

    // Configure GPIO pin for OPAMP input (example: PA0)
    __HAL_RCC_GPIOA_CLK_ENABLE();
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin = GPIO_PIN_0;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    // 1. Initialize OPAMP handle
    hopamp1.Instance = OPAMP1;

    // 2. Configure OPAMP in follower mode
    hopamp1.Init.Mode = OPAMP_FOLLOWER_MODE;
    hopamp1.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0; // PA0 for example
    hopamp1.Init.PowerMode = OPAMP_POWERMODE_NORMAL;
    hopamp1.Init.UserTrimming = OPAMP_TRIMMING_USER; // Use user trimming after calibration

    if (HAL_OPAMP_Init(&hopamp1) != HAL_OK)
    {
        Error_Handler();
    }

    // 3. Perform self-calibration to get trimming values
    if (HAL_OPAMP_SelfCalibrate(&hopamp1) != HAL_OK)
    {
        Error_Handler();
    }

    // 4. Start OPAMP
    if (HAL_OPAMP_Start(&hopamp1) != HAL_OK)
    {
        Error_Handler();
    }

    /* Add your processing code here */
}

void SystemClock_Config(void)
{
    // Configure system clock as needed
}

void Error_Handler(void)
{
    while(1);
}
hal_opamp_handle_t hopamp1;

void SystemClock_Config(void);
void Error_Handler(void);

int main(void)
{
    HAL_Init();
    SystemClock_Config();

    // Configure GPIO pin for OPAMP input
    HAL_RCC_GPIOA_CLK_ENABLE();
    hal_gpio_config_t GPIO_Config;
    GPIO_Config.Mode = HAL_GPIO_MODE_ANALOG;
    GPIO_Config.Pull = HAL_GPIO_PULL_NO;
    HAL_GPIO_Init(GPIOA, HAL_GPIO_PIN_0, &GPIO_Config);

    // 1. Initialize OPAMP handle
    hopamp1.Instance = OPAMP1;

    // 2. Configure OPAMP in follower mode
    hconfig.configuration_mode = HAL_OPAMP_MODE_FOLLOWER;
    if (HAL_OPAMP_SetConfig(&hopamp1, &hconfig) != HAL_OK)
    {
        Error_Handler();
    }

    if (HAL_OPAMP_Init(&hopamp1) != HAL_OK)
    {
        Error_Handler();
    }

    // 3. Perform self-calibration to get trimming values
    if (HAL_OPAMP_Calibrate(&hopamp1 , HAL_OPAMP_POWER_MODE_NORMAL) != HAL_OK)
    {
        Error_Handler();
    }

    // 4. Start OPAMP
    if (HAL_OPAMP_Start(&hopamp1) != HAL_OK)
    {
        Error_Handler();
    }

    /* Add your processing code here */
}

void SystemClock_Config(void)
{
    // Configure system clock as needed
}

void Error_Handler(void)
{
    while(1);
}

Key changes

  1. In HAL2, OPAMP configuration is provided using a configuration structure with HAL_OPAMP_SetConfig().

  2. The calibration API changes from HAL_OPAMP_SelfCalibrate() (HAL1) to HAL_OPAMP_Calibrate() (HAL2).